package com.woniu.goodsservice.controller;

import com.woniu.goodsservice.dto.AuctionDTO;
import com.woniu.goodsservice.dto.AuctionInforDTO;
import com.woniu.goodsservice.dto.AuctionDataDTO;
import com.woniu.goodsservice.dto.AuctionRoomDTO;
import com.woniu.goodsservice.entity.Auction;
import com.woniu.goodsservice.entity.Goods;
import com.woniu.goodsservice.service.AuctionDTOService;
import com.woniu.goodsservice.service.AuctionService;
import com.woniu.goodsservice.service.GoodsService;
import com.woniu.goodsservice.service.userservice;
import com.woniu.userservice.dto.BiddingsDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("auction")
public class AuctionController {

    @Resource
    private userservice uservice;
    @Resource
    private GoodsService goodsService;

    @Resource
    private AuctionDTOService auctionDTOService;
    @Resource
    private AuctionService auctionService;

    //查询拍品拍卖信息
    @GetMapping("/queryOne")
    public Auction queryOne(Integer id) {
        return auctionService.queryById(id);
    }

    //查询拍品所有信息
    @GetMapping("/selectOne")
    public AuctionRoomDTO selectOne(Integer id) {
        return auctionDTOService.selectById(id);
    }


    @RequestMapping("/list")
    public List<AuctionRoomDTO> list(){
        List<AuctionRoomDTO> list=auctionDTOService.findGoodsByPage();
        return list;
    }

    @GetMapping("/sort")
    public List<AuctionDTO> list(String sort){
        List<AuctionDTO> list=null;
        if (sort.equals("起拍价格")) {
            list = auctionDTOService.findGoodsStartprice();
        }else if (sort.equals("报名人数")){
            list=auctionDTOService.findGoodsWatchpeople();
        }else if (sort.equals("今日拍卖")){
            list=auctionDTOService.findGoodsDate();
        }
        return  list;
    }
    /**
     *
     * @param id
     * @return 用户出价信息
     */
    @RequestMapping("/userbidding")
    public  List<BiddingsDTO> userbidding(Integer id){
        Integer goodsid=id;
        List<BiddingsDTO> list=uservice.queryByGoodsid(goodsid);
        return list;
    }

    /**
     *
     * @param id
     * @return 拍品详情页面所需的数据
     */
    @RequestMapping("/auctioninfor")
    public AuctionInforDTO auctionInfor(Integer id){
        AuctionInforDTO auctionInforDTO=null;
        //根据拍品ID查询拍品数据
        Auction auction=auctionService.queryById(id);
        Double startprice=auction.getStartprice();

        //通过查询出的拍品数据获取商品ID，查询出商品数据
        Integer goodsid=auction.getGoodsid();
        Goods goods=goodsService.queryByGoodsid(goodsid);

        //根据商品ID查询报名人数
        Integer signuppeople=uservice.selectByGoodsid(goodsid);

        //根据商品id查询设置提醒的人数
        Integer reminderpeople=uservice.selectReminderpeople(goodsid);

        //根据商品ID查询出用户的出价信息，筛选出最大的价格增长获得现在的价格
        List<BiddingsDTO> list=uservice.queryByGoodsid(goodsid);
        Double temp=0.0;
        for (int i=0;i<list.size()-1;i++){
            if (temp<list.get(i).getSumprice()){
                temp=list.get(i).getSumprice();
            }
        }
        //1.现在的拍品价格
        auctionInforDTO.setNowprice(startprice+temp);
        //2.拍品名字
        auctionInforDTO.setGoodsname(goods.getGoodsname());
        //3.拍品图片路径
        auctionInforDTO.setImageurl(goods.getImageurl());
        //4.拍卖状态

        //5.竞价次数
        auctionInforDTO.setAuctionnumber(auction.getAuctionnumber());
        //6.拍卖时长
        auctionInforDTO.setDurationtime(auction.getDurationtime());
        //7.买方报名保证金
        auctionInforDTO.setBuyerdeposit(auction.getBuyerdeposit());
        //8.商家的保证金
        auctionInforDTO.setSellerdeposit(auction.getSellerdeposit());
        //9.报名人数
        auctionInforDTO.setSignuppeople(signuppeople);
        //10.设置提醒的人数
        auctionInforDTO.setReminderpeople(reminderpeople);
        //11.起拍价格
        auctionInforDTO.setStartprice(auction.getStartprice());
        //12.加价幅度
        auctionInforDTO.setPricerange(auction.getPricerange());
        //13.卖方保留价
        auctionInforDTO.setIntentionprice(auction.getIntentionprice());
        //14.卖方id
        auctionInforDTO.setUserid(goods.getUserid());

        return auctionInforDTO;
    }




    /*
     * Description : TODO  <br/> 后台获取拍卖物品信息
     * ChangeLog : 1. 创建 (2020/3/26 9:42 [黄晶]);
     * @param currentPage
     * @param pageSize
     * @return java.util.List<com.woniu.goodsservice.entity.Auction>
    */
    @GetMapping("/dataList")
    @ResponseBody
    public List<AuctionDataDTO> dataList(Integer currentPage, Integer pageSize){
      List<AuctionDataDTO> dataList=  auctionService.selectAuctionDataList(currentPage,pageSize);
        return dataList;
    }

}